package com.vdong.common.oauth.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vdong.common.oauth.common.enums.PlatformEnum;
import com.vdong.common.oauth.common.enums.SmsConstant;
import com.vdong.common.oauth.common.exception.WebBizException;
import com.vdong.common.oauth.common.util.ValidateUtils;
import com.vdong.common.oauth.service.SmsSendService;
import com.vdong.common.oauth.service.SmsVerificationCodeService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class SmsVerificationCodeServiceImpl implements SmsVerificationCodeService {

    private static final String SMS_VERIFICATION_CODE_KAY = "sms:verification:type_%s:code:platform_%s:phone_%s";


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Resource
    private SmsSendService smsSendService;


    @Override
    public String sendVerificationCodeDelAuth(String phone, PlatformEnum platform) {
        return sendVerificationCode(phone, SmsConstant.VerifyCodeType.DEL_AUTH, platform);
    }
    private String sendVerificationCode(String phone, SmsConstant.VerifyCodeType verifyCodeType, PlatformEnum platform) {
        if (StringUtils.isBlank(phone)) {
            throw new WebBizException("手机号不能为空!");
            //return new APIResult<>(ResultCodeEnum.FAIL, "手机号不能为空!");
        }
        boolean isMobile = ValidateUtils.isMobileFormat(phone, true, 11);
        if (!isMobile) {
            throw new WebBizException("手机号不正确，请重新输入!");
            //return new APIResult<>(ResultCodeEnum.FAIL, "手机号不正确，请重新输入!");
        }
        String code = RandomStringUtils.randomNumeric(6);
        String result = smsSendService.sendVerifyCode(phone, verifyCodeType, code, platform.getDesc());
        String key = String.format(SMS_VERIFICATION_CODE_KAY, verifyCodeType.getType(), platform.getType(), phone);
        redisTemplate.opsForValue().set(key, code, verifyCodeType.getExpiredTime(), TimeUnit.MINUTES);
        return result;
    }

    @Override
    public Boolean checkIsCorrectCode(String phone, SmsConstant.VerifyCodeType verifyCodeType, PlatformEnum platform, String code) {
        if (StringUtils.isBlank(phone)) {
            return Boolean.FALSE;
        }
        boolean isMobile = ValidateUtils.isMobileFormat(phone, true, 11);
        if (!isMobile) {
            return Boolean.FALSE;
        }
        if (StringUtils.isBlank(code)) {
            return Boolean.FALSE;
        }
        String key = String.format(SMS_VERIFICATION_CODE_KAY, verifyCodeType.getType(), platform.getType(), phone);
        if (!code.equalsIgnoreCase(redisTemplate.opsForValue().get(key))) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
